import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.microedition.lcdui.Display;
import javax.microedition.media.*;
import javax.microedition.media.control.VolumeControl;

public final class ci
        implements PlayerListener {

    public static final String a[] = {
            null, null, "/guiro.mid", "/maracas.mid", "/scale.mid", "/bird.mid", "/alert6.mid", "/alert7.mid"
    };
    private static ci b = new ci();

    private ci() {
    }

    public static synchronized void a(aw aw1, String s, int i) {
        a(aw1, s, i, b);
    }

    public static synchronized Player a(aw aw1, String s, int i, PlayerListener playerlistener) {
        String s1;
        String s2;
        if (!aq.a().f())
            return null;
        if (s.length() == 1 && Character.isDigit(s.charAt(0))) {
            int j;
            if ((j = s.charAt(0) - 48) == 1) {
                i = 15 * i;
                ce.a("vibrate: " + i);
                bh.a().vibrate(i);
                return null;
            }
            if (j > 1 && j < a.length)
                return a(ce.d(a[j]), i, "audio/midi", ((PlayerListener) (b)));
            else
                return null;
        }
        if (aw1 == null)
            return null;
        s1 = s.toLowerCase();
        s2 = "audio";
        if (s1.endsWith(".mid"))
            s2 = "audio/midi";
        else if (s1.endsWith(".mp3"))
            s2 = "audio/mpeg";
        else if (s1.endsWith(".wav"))
            s2 = "audio/x-wav";
        else if (s1.endsWith(".aac"))
            s2 = "audio/aac";
        else if (s1.endsWith(".amr"))
            s2 = "audio/amr";
        bw bw1;
        ByteArrayInputStream bytearrayinputstream;
        if ((bw1 = aw1.a(s)) != null) {
            return a(((InputStream) (bytearrayinputstream = new ByteArrayInputStream(bw1.a()))), i, s2, playerlistener);
        } else {
            ce.c("Sound resource not found, " + s);
            return null;
        }
    }

    public static synchronized Player a(InputStream inputstream, int i, String s, PlayerListener playerlistener) {
        Player player;
        if (ce.a(21) || ce.a(1))
            return null;
        if (i < 0)
            i = 0;
        else if (i > 100)
            i = 100;
        ce.a("play sound: content-type=" + s + ", volume=" + i);
        player = null;
        try {
            (player = Manager.createPlayer(inputstream, s)).realize();
            player.prefetch();
            if (playerlistener != null)
                player.addPlayerListener(playerlistener);
            VolumeControl volumecontrol;
            if ((volumecontrol = (VolumeControl) player.getControl("VolumeControl")) != null && volumecontrol.getLevel() != i)
                volumecontrol.setLevel(i);
            if (playerlistener != null)
                player.start();
            return player;
        } catch (Throwable throwable) {
            ce.a("Sound.play(): " + throwable);
            throwable.printStackTrace();
        } finally {
            try {
                if (player != null)
                    player.close();
            }
            catch (Exception _ex) {
            }
        }
        return null;
    }

    public final void playerUpdate(Player player, String s, Object obj) {
        if (s == "endOfMedia" || s == "error")
            player.close();
    }

}